package com.example.hp.yuejian5.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.RetrofitListener.RetrofitListener;
import com.example.hp.yuejian5.bean.RegisterBean;
import com.example.hp.yuejian5.model.RegisterModel;

public class RegisterActivity extends AppCompatActivity{
    private Button register_button;
    private Button r_button;
    private EditText name, password,tel,email;
    private RetrofitListener<RegisterBean> listener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initEvents();
        listener=new RetrofitListener<RegisterBean>(){
            @Override
            public void onSuccess(RegisterBean registerBean, int flag) {
                if(registerBean.getSuccess().equals("1"))
                {
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
                else
                    {
                        Toast.makeText(RegisterActivity.this,"用户名已被注册",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {
                Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
            }
        };
    }
    private void initView(){
        register_button=findViewById(R.id.register_button);
        r_button=findViewById(R.id.r_button);
        name=findViewById(R.id.username);
        password=findViewById(R.id.password);
        tel =findViewById(R.id.usertel);
        email=findViewById(R.id.useremail);
    }
private void initEvents(){
        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
        r_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                return_b();
            }
        });
}
private void register(){
    RegisterModel registerModel=new RegisterModel();
    registerModel.userRegister(name.getText().toString().trim(),password.getText().toString().trim(),email.getText().toString().trim(),tel.getText().toString().trim(),listener);
}
private  void return_b(){
    Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
    startActivity(intent);
    finish();
}
}
